package com.gewu.teday.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.gewu.teday.R
import com.gewu.teday.bean.ItemIedaBenans
import com.gewu.teday.bean.TiXianItemBean
import com.gewu.teday.fragmnet.ZhengTiFragment
import kotlinx.android.parcel.Parcelize

class TixianListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var list = ArrayList<TiXianItemBean>()
    private var onclicks: Onclick? = null;

    fun addList(itemBenan: TiXianItemBean) {
        list.add(itemBenan)
        list.sortBy { it.time }
        list.reverse()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate =
            LayoutInflater.from(parent.context).inflate(R.layout.tixian_item, parent, false)
        return Hoder(inflate)
    }

    fun setClick(onclick: Onclick) {
        this.onclicks = onclick
    }

    interface Onclick {
        fun remove(index: String)
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val hoder = holder as Hoder
        hoder.bankName?.text = "提现至\t${list[position].bankName}(尾号${list[position].bankNum})"
        hoder.bankMoney?.text = "¥\t${list[position].bankMoney}"
        hoder.bankTime?.text = list[position].time
        hoder.content?.setOnLongClickListener(object : View.OnLongClickListener{
            @SuppressLint("NewApi")
            override fun onLongClick(p0: View?): Boolean {
                onclicks?.remove(list[position].time)
                list.removeIf {
                    it.time==list[position].time
                }
                return true
            }
        })
    }

    override fun getItemCount(): Int = if (list.size == 0) 0 else list.size

    class Hoder(inflate: View?) : RecyclerView.ViewHolder(inflate!!) {
        val bankName: TextView? = inflate?.findViewById<TextView>(R.id.bankName)
        var bankMoney: TextView? = inflate?.findViewById<TextView>(R.id.bankMoney)
        var bankTime: TextView? = inflate?.findViewById<TextView>(R.id.bankTime)
        var content: LinearLayout? = inflate?.findViewById<LinearLayout>(R.id.cotent)
    }
}

@Parcelize
data class TixianListBean(var list: ArrayList<TiXianItemBean>) : Parcelable
